package stz.flash.option.app;

import net.rim.device.api.system.CodeModuleManager;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import stz.flash.Info;
import stz.flash.main.CloseHandle;
import stz.flash.option.OptionLabel;
import stz.flash.option.OptionScreen;
import stz.flash.string.OptionInformation;
import stz.flash.string.OptionString;
import blackberry.util.field.DjButtonField;
import blackberry.util.field.DjDialog;
import blackberry.util.field.DjLabelField;

public class UninstallField extends DjButtonField implements FieldChangeListener {

	public UninstallField() {
		super(DjLabelField.createSimpleLabel(OptionString.UNINSTAL_FIELD),
				OptionLabel.createInfoLabel(OptionInformation.UNINSTALL_FIELD),
				OptionString.BUTTON_NAME_OF_UNINSTALL_FIELD);
		setChangeListener(this);
	}

	public void fieldChanged(Field field, int context) {
		new DeleteDialog(
				OptionInformation.DELETE_DIALOG, 
				new String[] {OptionString.D_DELETE, OptionString.D_CANCEL},
				1)
		.pushGlobalDialog();
	}
	
	class DeleteDialog extends DjDialog {
		
		public DeleteDialog(String message, String[] buttonNames, int initIndex) {
			super(message, buttonNames, initIndex);
		}

		public void buttonChanged(String buttonName) {

			if (buttonName.endsWith(OptionString.D_DELETE)) {
				
				CodeModuleManager.deleteModuleEx(
						CodeModuleManager.getModuleHandle(Info.FILE_NAME_OF_APPLICATION), 
						true);
				OptionScreen.getIns().close();
				CloseHandle.close();
			} 
		}
	}
}